﻿using Newtonsoft.Json.Linq;
using StackExchange.Redis;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Data
{
    /// <summary>
    /// Redis数据库，依赖StackExchange.Redis包
    /// </summary>
    class RedisConnection
    {
        private static ConnectionMultiplexer _redis;

        public static ConnectionMultiplexer Connect(string configuration)
        {
            if (_redis == null || !_redis.IsConnected)
            {
                // 创建连接
                _redis = ConnectionMultiplexer.Connect(configuration);
            }
            return _redis;
        }
    }

    public class RedisHelper
    {
        private readonly IDatabase _database;

        public RedisHelper()
        {
            //创建连接
            var connection = RedisConnection.Connect("localhost:6379");
            //获取数据库访问对象
            _database = connection.GetDatabase();
        }

        #region 字符串

        public void SetValue(string key, string value)
        {
            _database.StringSet(key, value);
        }

        public string GetValue(string key)
        {
            return _database.StringGet(key);
        }

        #endregion

        #region List列表

        /// <summary>
        /// 压栈
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public long ListPush(string key, string value, bool right = true)
        {
            if (right)
                return _database.ListRightPush(key, value);
            else
                return _database.ListLeftPush(key, value);
        }

        /// <summary>
        /// 弹出
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public RedisValue ListPop(string key, bool right = false)
        {
            if (right)
                return _database.ListRightPop(key);
            else
                return _database.ListLeftPop(key);
        }

        /// <summary>
        /// 返回所有
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public RedisValue[] ListGetAll(string key)
        {
            return _database.ListRange(key);
        }
        /// <summary>
        /// 获取长度
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public long ListLength(string key)
        {
            return _database.ListLength(key);
        }
        /// <summary>
        /// 移除元素
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public long ListLength(string key, string value)
        {
            return _database.ListRemove(key, value);
        }
        /// <summary>
        /// 按下标更新
        /// </summary>
        /// <param name="key"></param>
        /// <param name="index"></param>
        /// <param name="value"></param>
        public void ListLength(string key, long index, string value)
        {
            _database.ListSetByIndex(key, index, value);
        }

        #endregion

        #region Hash哈希

        /// <summary>
        /// 添加或更新 Hash 中的字段
        /// </summary>
        /// <param name="key"></param>
        /// <param name="hashEntries"></param>
        public void HashSet(string key, HashEntry[] hashEntries)
        {
            _database.HashSet(key, hashEntries);

            //db.HashSet("user:1000", new HashEntry[] {
            //    new HashEntry("username", "john_doe"),
            //    new HashEntry("email", "john@example.com"),
            //    new HashEntry("age", 30)
            //});
        }

        /// <summary>
        /// 获取 Hash 中的单个字段
        /// </summary>
        /// <param name="key"></param>
        /// <param name="filedName"></param>
        /// <returns></returns>
        public RedisValue HashGet(string key, string filedName)
        {
            return _database.HashGet(key, filedName);
        }

        /// <summary>
        /// 获取 Hash 中的所有字段
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public HashEntry[] HashGetAll(string key)
        {
            return _database.HashGetAll(key);

            //获取 Hash 中的所有字段
            //HashEntry[] userFields = db.HashGetAll("user:1000");
            //foreach (var field in userFields)
            //{
            //    Console.WriteLine($"{field.Name}: {field.Value}");
            //}
        }

        /// <summary>
        /// 删除 Hash 中的字段
        /// </summary>
        /// <param name="key"></param>
        /// <param name="field"></param>
        public void HashDelete(string key, string field)
        {
            _database.HashDelete(key, field);
        }

        /// <summary>
        /// 判断 Hash 中是否存在某个字段
        /// </summary>
        /// <param name="key"></param>
        /// <param name="field"></param>
        public void HashExists(string key, string field)
        {
            _database.HashExists(key, field);
        }

        /// <summary>
        /// 获取 Hash 中的字段数量
        /// </summary>
        /// <param name="key"></param>
        public void HashLength(string key)
        {
            _database.HashLength(key);
        }

        /// <summary>
        /// 增加哈希字段中的数值
        /// </summary>
        /// <param name="key"></param>
        /// <param name="field"></param>
        /// <param name="isAdd"></param>
        /// <param name="num"></param>
        public void HashIncrement(string key, string field, bool isAdd= true, int num = 1)
        {
            if (isAdd)
                _database.HashIncrement(key, field, num);
            else
                _database.HashDecrement(key, field, num);
        }

        #endregion

        // 其他操作...

        public static void Test()
        {
            RedisHelper redisExample = new RedisHelper();

            // 设置值
            redisExample.SetValue("myKey", "Hello, Redis!");

            // 获取值
            string value = redisExample.GetValue("myKey");
            Console.WriteLine(value); // 输出: Hello, Redis!
        }
    }

    public class RedisService
    {
        ConnectionMultiplexer _redis_;
        ConnectionMultiplexer _redis
        {
            get
            {
                if (_redis_ == null || !_redis_.IsConnected)
                    _redis_ = ConnectionMultiplexer.Connect("localhost:");
                return _redis_;
            }
        }

        #region 字符串

        public void SetValue(string key, string value)
        {
            // 获取数据库访问对象
            IDatabase db = _redis_.GetDatabase();
            db.StringSet(key, value);
        }

        public string GetValue(string key)
        {
            IDatabase db = _redis_.GetDatabase();
            return db.StringGet(key);
        }

        #endregion


        #region List列表

        /// <summary>
        /// 压栈
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public long ListPush(string key, string value, bool right = true)
        {
            IDatabase db = _redis_.GetDatabase();
            if (right)
                return db.ListRightPush(key, value);
            else
                return db.ListLeftPush(key, value);
        }

        /// <summary>
        /// 弹出
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public RedisValue ListPop(string key, bool right = false)
        {
            IDatabase db = _redis_.GetDatabase();
            if (right)
                return db.ListRightPop(key);
            else
                return db.ListLeftPop(key);
        }

        /// <summary>
        /// 返回所有
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public RedisValue[] ListGetAll(string key)
        {
            IDatabase db = _redis_.GetDatabase();
            return db.ListRange(key);
        }
        /// <summary>
        /// 获取长度
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public long ListLength(string key)
        {
            IDatabase db = _redis_.GetDatabase();
            return db.ListLength(key);
        }
        /// <summary>
        /// 移除元素
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public long ListLength(string key, string value)
        {
            IDatabase db = _redis_.GetDatabase();
            return db.ListRemove(key, value);
        }
        /// <summary>
        /// 按下标更新
        /// </summary>
        /// <param name="key"></param>
        /// <param name="index"></param>
        /// <param name="value"></param>
        public void ListLength(string key, long index, string value)
        {
            IDatabase db = _redis_.GetDatabase();
            db.ListSetByIndex(key, index, value);
        }

        #endregion
    }
}
